package chap11;


import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Classex2 {
    public static void main(String[] args) {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter("sort1.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedWriter F = new BufferedWriter(fileWriter);
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入任意个整数：");
        String a = scan.nextLine();
        try {
            F.write(a);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            F.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            F.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }


        FileReader fileReader = null;
        try {
            fileReader = new FileReader("sort1.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader R1 = new BufferedReader(fileReader);
        String str = null;
        try {
            str = R1.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        StringTokenizer tokenizer = new StringTokenizer(str);
        int[] nums = new int[tokenizer.countTokens()];
        int y = 0;
        while (tokenizer.hasMoreTokens()) {
            nums[y] = Integer.parseInt(tokenizer.nextToken());
            y++;
        }


        Classex2.insertSort(nums);
        String result = "";
        for (int x=0; x<nums.length; x++) {
            result += nums[x] + " ";
        }
        try {
            F.write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            F.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

        private static void insertSort(int[] array) {
            for (int i = 1; i < array.length; i++) {
                int temp = array[i];
                for (int j = i - 1; j >= 0 && temp < array[j]; j--) {
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
            }

        }

}
